package com.pd.backend.controller;

import com.pd.backend.dtos.LoginDto;
import com.pd.backend.vo.LoginVo;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.service.UserService;
import com.pd.backend.utils.RedisUtils;
import com.pd.backend.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 管理登录控制层
 * @Auther : 菜鸡编程
 * @Date:2023/1/10
 * @Description:
 * @VERSON:1.8
 */
@RestController
@Slf4j
@RequestMapping(path = "/user")
@Api(tags = "用户登录")
public class LoginUserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping("/login")
    @ApiOperation("登录")
    public ResponseResult<LoginVo> userLogin(@RequestBody LoginDto loginDto){
        return userService.userLogin(loginDto);
    }



    /**
     * 发送验证码
     *
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/sendMsg")
    @ApiOperation("获取验证码")
    public ResponseResult<String> send(String account) throws Exception {
        //1.生成验证码并保存Redis
        String code = ValidateCodeUtils.generateCode(4);
        //创建redis的key
        String key = RedisUtils.createKey("app_login_code", account);
        //存入redis 指定过期时间为5分钟
        stringRedisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
        log.warn("{}", code);
        return ResponseResult.okResult(code);
    }
}